<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>MultiIndex to Bimap Path - Bidirectional Map</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Bimap">
<link rel="up" href="../examples.html" title="Examples">
<link rel="prev" href="mighty_bimap.html" title="Mighty Bimap">
<link rel="next" href="multiindex_to_bimap_path___hashed_indices.html" title="MultiIndex to Bimap Path - Hashed indices">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="mighty_bimap.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="multiindex_to_bimap_path___hashed_indices.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map"></a><a class="link" href="multiindex_to_bimap_path___bidirectional_map.html" title="MultiIndex to Bimap Path - Bidirectional Map">MultiIndex
      to Bimap Path - Bidirectional Map</a>
</h3></div></div></div>
<p>
        This is example 4 in Boost.MultiIndex documentation.
      </p>
<div class="blurb">
<div class="titlepage"><div><div><p class="title"><b></b></p></div></div></div>
<p>
        This example shows how to construct a bidirectional map with multi_index_container.
        By a bidirectional map we mean a container of elements of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">FromType</span><span class="special">,</span><span class="keyword">const</span> <span class="identifier">ToType</span><span class="special">&gt;</span></code> such that no two elements exists with
        the same first or second value (<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span></code> only
        guarantees uniqueness of the first member). Fast look-up is provided for
        both keys. The program features a tiny Spanish-English dictionary with on-line
        query of words in both languages.
      </p>
</div>
<h5>
<a name="boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.h0"></a>
        <span class="phrase"><a name="boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.boost_multiindex"></a></span><a class="link" href="multiindex_to_bimap_path___bidirectional_map.html#boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.boost_multiindex">Boost.MultiIndex</a>
      </h5>
<p>
        <a href="../../../../example/mi_to_b_path/mi_bidirectional_map.cpp" target="_top">Go to source
        code</a>
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">tokenizer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multi_index_container</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multi_index</span><span class="special">/</span><span class="identifier">key_extractors</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multi_index</span><span class="special">/</span><span class="identifier">ordered_index</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">;</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multi_index</span><span class="special">;</span>

<span class="comment">// tags for accessing both sides of a bidirectional map</span>

<span class="keyword">struct</span> <span class="identifier">from</span> <span class="special">{};</span>
<span class="keyword">struct</span> <span class="identifier">to</span>   <span class="special">{};</span>

<span class="comment">// The class template bidirectional_map wraps the specification</span>
<span class="comment">// of a bidirectional map based on multi_index_container.</span>

<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">FromType</span><span class="special">,</span><span class="keyword">typename</span> <span class="identifier">ToType</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">bidirectional_map</span>
<span class="special">{</span>
    <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">FromType</span><span class="special">,</span><span class="identifier">ToType</span><span class="special">&gt;</span> <span class="identifier">value_type</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="identifier">multi_index_container</span><span class="special">&lt;</span>
        <span class="identifier">value_type</span><span class="special">,</span>
        <span class="identifier">indexed_by</span>
        <span class="special">&lt;</span>
            <span class="identifier">ordered_unique</span>
            <span class="special">&lt;</span>
                <span class="identifier">tag</span><span class="special">&lt;</span><span class="identifier">from</span><span class="special">&gt;,</span> <span class="identifier">member</span><span class="special">&lt;</span><span class="identifier">value_type</span><span class="special">,</span><span class="identifier">FromType</span><span class="special">,&amp;</span><span class="identifier">value_type</span><span class="special">::</span><span class="identifier">first</span><span class="special">&gt;</span>
            <span class="special">&gt;,</span>
            <span class="identifier">ordered_unique</span>
            <span class="special">&lt;</span>
                <span class="identifier">tag</span><span class="special">&lt;</span><span class="identifier">to</span><span class="special">&gt;,</span> <span class="identifier">member</span><span class="special">&lt;</span><span class="identifier">value_type</span><span class="special">,</span><span class="identifier">ToType</span><span class="special">,&amp;</span><span class="identifier">value_type</span><span class="special">::</span><span class="identifier">second</span><span class="special">&gt;</span>
            <span class="special">&gt;</span>
        <span class="special">&gt;</span>

  <span class="special">&gt;</span> <span class="identifier">type</span><span class="special">;</span>

<span class="special">};</span>

<span class="comment">// A dictionary is a bidirectional map from strings to strings</span>

<span class="keyword">typedef</span> <span class="identifier">bidirectional_map</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">dictionary</span><span class="special">;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
    <span class="identifier">dictionary</span> <span class="identifier">d</span><span class="special">;</span>

    <span class="comment">// Fill up our microdictionary.</span>
    <span class="comment">// first members Spanish, second members English.</span>

    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">(</span><span class="string">"hola"</span><span class="special">,</span><span class="string">"hello"</span><span class="special">));</span>
    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">(</span><span class="string">"adios"</span><span class="special">,</span><span class="string">"goodbye"</span><span class="special">));</span>
    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">(</span><span class="string">"rosa"</span><span class="special">,</span><span class="string">"rose"</span><span class="special">));</span>
    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">(</span><span class="string">"mesa"</span><span class="special">,</span><span class="string">"table"</span><span class="special">));</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"enter a word"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">word</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">getline</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cin</span><span class="special">,</span><span class="identifier">word</span><span class="special">);</span>

    <span class="comment">// search the queried word on the from index (Spanish)</span>

    <span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">get</span><span class="special">&lt;</span><span class="identifier">from</span><span class="special">&gt;().</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">word</span><span class="special">);</span>

    <span class="keyword">if</span><span class="special">(</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">end</span><span class="special">()</span> <span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// the second part of the element is the equivalent in English</span>

        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">word</span> <span class="special">&lt;&lt;</span> <span class="string">" is said "</span>
                  <span class="special">&lt;&lt;</span> <span class="identifier">it</span><span class="special">-&gt;</span><span class="identifier">second</span> <span class="special">&lt;&lt;</span> <span class="string">" in English"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">else</span>
    <span class="special">{</span>
        <span class="comment">// word not found in Spanish, try our luck in English</span>

        <span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">index_iterator</span><span class="special">&lt;</span><span class="identifier">to</span><span class="special">&gt;::</span><span class="identifier">type</span> <span class="identifier">it2</span> <span class="special">=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">get</span><span class="special">&lt;</span><span class="identifier">to</span><span class="special">&gt;().</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">word</span><span class="special">);</span>
        <span class="keyword">if</span><span class="special">(</span> <span class="identifier">it2</span> <span class="special">!=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">get</span><span class="special">&lt;</span><span class="identifier">to</span><span class="special">&gt;().</span><span class="identifier">end</span><span class="special">()</span> <span class="special">)</span>
        <span class="special">{</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">word</span> <span class="special">&lt;&lt;</span> <span class="string">" is said "</span>
                      <span class="special">&lt;&lt;</span> <span class="identifier">it2</span><span class="special">-&gt;</span><span class="identifier">first</span> <span class="special">&lt;&lt;</span> <span class="string">" in Spanish"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="keyword">else</span>
        <span class="special">{</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"No such word in the dictionary"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
        <span class="special">}</span>
    <span class="special">}</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
<h5>
<a name="boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.h1"></a>
        <span class="phrase"><a name="boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.boost_bimap"></a></span><a class="link" href="multiindex_to_bimap_path___bidirectional_map.html#boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.boost_bimap">Boost.Bimap</a>
      </h5>
<p>
        <a href="../../../../example/mi_to_b_path/bidirectional_map.cpp" target="_top">Go to source
        code</a>
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">tokenizer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">bimap</span><span class="special">/</span><span class="identifier">bimap</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bimaps</span><span class="special">;</span>

<span class="comment">// A dictionary is a bidirectional map from strings to strings</span>

<span class="keyword">typedef</span> <span class="identifier">bimap</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">dictionary</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">translation</span><span class="special">;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
    <span class="identifier">dictionary</span> <span class="identifier">d</span><span class="special">;</span>

    <span class="comment">// Fill up our microdictionary.</span>
    <span class="comment">// first members Spanish, second members English.</span>

    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">translation</span><span class="special">(</span><span class="string">"hola"</span> <span class="special">,</span><span class="string">"hello"</span>  <span class="special">));</span>
    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">translation</span><span class="special">(</span><span class="string">"adios"</span><span class="special">,</span><span class="string">"goodbye"</span><span class="special">));</span>
    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">translation</span><span class="special">(</span><span class="string">"rosa"</span> <span class="special">,</span><span class="string">"rose"</span>   <span class="special">));</span>
    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">translation</span><span class="special">(</span><span class="string">"mesa"</span> <span class="special">,</span><span class="string">"table"</span>  <span class="special">));</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"enter a word"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">word</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">getline</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cin</span><span class="special">,</span><span class="identifier">word</span><span class="special">);</span>

    <span class="comment">// search the queried word on the from index (Spanish)</span>

    <span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">left_const_iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">left</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">word</span><span class="special">);</span>

    <span class="keyword">if</span><span class="special">(</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">left</span><span class="special">.</span><span class="identifier">end</span><span class="special">()</span> <span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// the second part of the element is the equivalent in English</span>

        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">word</span> <span class="special">&lt;&lt;</span> <span class="string">" is said "</span>
                  <span class="special">&lt;&lt;</span> <span class="identifier">it</span><span class="special">-&gt;</span><span class="identifier">second</span> <a class="co" name="boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.c0" href="multiindex_to_bimap_path___bidirectional_map.html#boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.c1"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a>
                  <span class="special">&lt;&lt;</span> <span class="string">" in English"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">else</span>
    <span class="special">{</span>
        <span class="comment">// word not found in Spanish, try our luck in English</span>

        <span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">right_const_iterator</span> <span class="identifier">it2</span> <span class="special">=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">word</span><span class="special">);</span>
        <span class="keyword">if</span><span class="special">(</span> <span class="identifier">it2</span> <span class="special">!=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">end</span><span class="special">()</span> <span class="special">)</span>
        <span class="special">{</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">word</span> <span class="special">&lt;&lt;</span> <span class="string">" is said "</span>
                      <span class="special">&lt;&lt;</span> <span class="identifier">it2</span><span class="special">-&gt;</span><span class="identifier">second</span> <a class="co" name="boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.c2" href="multiindex_to_bimap_path___bidirectional_map.html#boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.c3"><img src="../../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a>
                      <span class="special">&lt;&lt;</span> <span class="string">" in Spanish"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="keyword">else</span>
        <span class="special">{</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"No such word in the dictionary"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
        <span class="special">}</span>
    <span class="special">}</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
<div class="calloutlist"><table border="0" summary="Callout list">
<tr>
<td width="5%" valign="top" align="left"><p><a name="boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.c1"></a><a href="#boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.c0"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td>
<td valign="top" align="left"><p>
            <code class="computeroutput"><span class="identifier">it</span></code> is an iterator of
            the left view, so <code class="computeroutput"><span class="identifier">it</span><span class="special">-&gt;</span><span class="identifier">second</span></code>
            refers to the right element of the relation, the word in english
          </p></td>
</tr>
<tr>
<td width="5%" valign="top" align="left"><p><a name="boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.c3"></a><a href="#boost_bimap.examples.multiindex_to_bimap_path___bidirectional_map.c2"><img src="../../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td>
<td valign="top" align="left"><p>
            <code class="computeroutput"><span class="identifier">it2</span></code> is an iterator of
            the right view, so <code class="computeroutput"><span class="identifier">it2</span><span class="special">-&gt;</span><span class="identifier">second</span></code>
            refers to the left element of the relation, the word in spanish
          </p></td>
</tr>
</table></div>
<p>
        Or better, using tags...
      </p>
<p>
        <a href="../../../../example/mi_to_b_path/tagged_bidirectional_map.cpp" target="_top">Go to
        source code</a>
      </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">bimap</span><span class="special">/</span><span class="identifier">bimap</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bimaps</span><span class="special">;</span>

<span class="comment">// tags</span>

<span class="keyword">struct</span> <span class="identifier">spanish</span> <span class="special">{};</span>
<span class="keyword">struct</span> <span class="identifier">english</span> <span class="special">{};</span>

<span class="comment">// A dictionary is a bidirectional map from strings to strings</span>

<span class="keyword">typedef</span> <span class="identifier">bimap</span>
<span class="special">&lt;</span>
    <span class="identifier">tagged</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span><span class="identifier">spanish</span> <span class="special">&gt;,</span> <span class="identifier">tagged</span><span class="special">&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span><span class="identifier">english</span> <span class="special">&gt;</span>

<span class="special">&gt;</span> <span class="identifier">dictionary</span><span class="special">;</span>

<span class="keyword">typedef</span> <span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">translation</span><span class="special">;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
    <span class="identifier">dictionary</span> <span class="identifier">d</span><span class="special">;</span>

    <span class="comment">// Fill up our microdictionary. </span>
    <span class="comment">// first members Spanish, second members English.</span>

    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">translation</span><span class="special">(</span><span class="string">"hola"</span> <span class="special">,</span><span class="string">"hello"</span>  <span class="special">));</span>
    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">translation</span><span class="special">(</span><span class="string">"adios"</span><span class="special">,</span><span class="string">"goodbye"</span><span class="special">));</span>
    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">translation</span><span class="special">(</span><span class="string">"rosa"</span> <span class="special">,</span><span class="string">"rose"</span>   <span class="special">));</span>
    <span class="identifier">d</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">translation</span><span class="special">(</span><span class="string">"mesa"</span> <span class="special">,</span><span class="string">"table"</span>  <span class="special">));</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"enter a word"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">word</span><span class="special">;</span>
    <span class="identifier">std</span><span class="special">::</span><span class="identifier">getline</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cin</span><span class="special">,</span><span class="identifier">word</span><span class="special">);</span>

    <span class="comment">// search the queried word on the from index (Spanish) */</span>

    <span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">map_by</span><span class="special">&lt;</span><span class="identifier">spanish</span><span class="special">&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">it</span> <span class="special">=</span>
        <span class="identifier">d</span><span class="special">.</span><span class="identifier">by</span><span class="special">&lt;</span><span class="identifier">spanish</span><span class="special">&gt;().</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">word</span><span class="special">);</span>

    <span class="keyword">if</span><span class="special">(</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">by</span><span class="special">&lt;</span><span class="identifier">spanish</span><span class="special">&gt;().</span><span class="identifier">end</span><span class="special">()</span> <span class="special">)</span>
    <span class="special">{</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">word</span> <span class="special">&lt;&lt;</span> <span class="string">" is said "</span>
                  <span class="special">&lt;&lt;</span> <span class="identifier">it</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">&lt;</span><span class="identifier">english</span><span class="special">&gt;()</span> <span class="special">&lt;&lt;</span> <span class="string">" in English"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="special">}</span>
    <span class="keyword">else</span>
    <span class="special">{</span>
        <span class="comment">// word not found in Spanish, try our luck in English</span>

        <span class="identifier">dictionary</span><span class="special">::</span><span class="identifier">map_by</span><span class="special">&lt;</span><span class="identifier">english</span><span class="special">&gt;::</span><span class="identifier">const_iterator</span> <span class="identifier">it2</span> <span class="special">=</span>
            <span class="identifier">d</span><span class="special">.</span><span class="identifier">by</span><span class="special">&lt;</span><span class="identifier">english</span><span class="special">&gt;().</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">word</span><span class="special">);</span>

        <span class="keyword">if</span><span class="special">(</span> <span class="identifier">it2</span> <span class="special">!=</span> <span class="identifier">d</span><span class="special">.</span><span class="identifier">by</span><span class="special">&lt;</span><span class="identifier">english</span><span class="special">&gt;().</span><span class="identifier">end</span><span class="special">()</span> <span class="special">)</span>
        <span class="special">{</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">word</span> <span class="special">&lt;&lt;</span> <span class="string">" is said "</span>
                      <span class="special">&lt;&lt;</span> <span class="identifier">it2</span><span class="special">-&gt;</span><span class="identifier">get</span><span class="special">&lt;</span><span class="identifier">spanish</span><span class="special">&gt;()</span> <span class="special">&lt;&lt;</span> <span class="string">" in Spanish"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
        <span class="special">}</span>
        <span class="keyword">else</span>
        <span class="special">{</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"No such word in the dictionary"</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
        <span class="special">}</span>
    <span class="special">}</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2006-2012 Matias Capeletto<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="mighty_bimap.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../examples.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="multiindex_to_bimap_path___hashed_indices.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
